package edu.neusoft.yuejian_09.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import edu.neusoft.yuejian_09.R;
import edu.neusoft.yuejian_09.base.Constant;
import edu.neusoft.yuejian_09.base.RetrofitListener;
import edu.neusoft.yuejian_09.bean.Collection;
import edu.neusoft.yuejian_09.bean.Contest;
import edu.neusoft.yuejian_09.model.CollectModel;
import edu.neusoft.yuejian_09.model.ContestModel;
import it.sephiroth.android.library.picasso.Picasso;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static edu.neusoft.yuejian_09.base.Constant.PHOTO_URL;


public class ConInfoActivity extends Activity implements RetrofitListener {

    private TextView c_name,address,date,phone,info;
    private ImageView img_return,img_collect,pic;
    private Button callphone;
    private int contest_id;
    private String contest_name;
    private String contest_address;
    private String contest_time;
    private int contest_phone;
    private String contest_intro;
    private String contest_pic;
    private int user_id;
    private Collection collectResult;
    private String coresult="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_con_info);
        initViews();
        initEvents();
        initData();
    }

    public void initViews(){
        c_name=(TextView)findViewById(R.id.name);
        address=(TextView)findViewById(R.id.address);
        date=(TextView)findViewById(R.id.date);
        phone=(TextView)findViewById(R.id.phone);
        info=(TextView)findViewById(R.id.info);
        img_return=(ImageView) findViewById(R.id.img_return);
        img_collect=(ImageView) findViewById(R.id.img_collect);
        pic=(ImageView) findViewById(R.id.pic);
        callphone=(Button) findViewById(R.id.callphone);
    }

    public void initEvents(){

        img_return.setOnClickListener(new View.OnClickListener() {//返回
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        img_collect.setOnClickListener(new View.OnClickListener() {//收藏
            @Override
            public void onClick(View v) {
                collectContest();
            }
        });

    }

    public void initData(){

        getContestInfo();
        getUserId();
        checkCollect();

    }
    //获取比赛信息数据
    public void getContestInfo(){
        Bundle bundle = getIntent().getExtras();
        contest_id = bundle.getInt("contest_id");
        ContestModel contestModel = new ContestModel();
        contestModel.getContestById(contest_id,this);
    }

    //获取个人用户信息
    public void getUserId(){
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getInt("user_id", 0);
    }

    //收藏比赛
    private void collectContest( ){
        CollectModel collectModel = new CollectModel();
        collectModel.collectContest(user_id,contest_id,this);
    }

    //检查收藏状态
    private void checkCollect(){
        CollectModel collectModel = new CollectModel();
        collectModel.isCollected(user_id,contest_id,2,this);

    }

    //刷新
    private void refresh(){
        checkCollect();
    }

    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag){
            case Constant.CONTESTBYID:
                Contest contest=(Contest)o;
                contest_name = contest.getContest_name();
                System.out.printf("contest_name");
                contest_pic =PHOTO_URL+contest.getContest_pic();
                contest_intro=contest.getContest_intro();
                contest_address=contest.getContest_address();
                contest_time=contest.getContest_regist_time();
                contest_phone=contest.getContest_phone();
                c_name.setText(contest_name);
                info.setText(contest_intro);
                address.setText(contest_address);
                date.setText(contest_time);
                phone.setText(contest_phone+"");
                if (contest_pic != "") {
                    Picasso.with(ConInfoActivity.this)
                            .load(contest_pic)
                            .into(pic);
                    pic.setScaleType(ImageView.ScaleType.FIT_XY);
                } else {
                    pic.setImageResource(R.drawable.no_pic);
                }
                break;
            case Constant.COLLECTCONTEST:
                Collection collection=(Collection)o;
                if ("yes".equals(coresult)){
                    Toast.makeText(ConInfoActivity.this,"取消收藏成功！",Toast.LENGTH_SHORT).show();
                    img_collect.setImageResource(R.drawable.ic_collect);
                }else{
                    Toast.makeText(ConInfoActivity.this,"收藏成功！",Toast.LENGTH_SHORT).show();
                    img_collect.setImageResource(R.drawable.ic_collect1);
                }
                refresh();
                break;
            case Constant.COLLECTSTATE:
                Collection collectstate=(Collection)o;
                String result = collectstate.getCollected();
                System.out.printf(result);
                if ("0".equals(result)){
                    img_collect.setImageResource(R.drawable.ic_collect);
                    coresult = "no";
                }else {
                    coresult = "yes";
                    img_collect.setImageResource(R.drawable.ic_collect1);
                }
                break;
        }


    }
    @Override
    public void onFail(){ Toast.makeText(ConInfoActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}
}
